﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ALiteTeam.PackageTracer.Data;
using System.IO;
using Newtonsoft.Json;
using System.Data.Linq;
using System.Linq;
using System.Collections.Generic;

namespace ALiteTeam.PackageTracer.Core.Impl
{
    public class OrderOp : IOrderOperate
    {
        private ExpressTracerDB _DbContext;
        public OrderOp()
        {
            _DbContext = ExpressTracerDB.Instance;
            LogWriter = new GeneralLogWriter();
        }

        public ILogWrite LogWriter { get; set; }

        public void AddOrder(DeliveryOrder order)
        {
            try
            {
                _DbContext.DeliveryOrders.InsertOnSubmit(order);
                _DbContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw LogWriter.Write(ex);
            }
        }

        public void UpdateOrder(DeliveryOrder order, bool isDetached = false)
        {
            try
            {
                if (isDetached == true)
                {
                    _DbContext.DeliveryOrders.Attach(order);                    
                }

                //_DbContext.Refresh(RefreshMode.KeepChanges, order);
                _DbContext.SubmitChanges();
            }
            catch (ChangeConflictException ex)
            {
                _DbContext.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                _DbContext.SubmitChanges();

                throw LogWriter.Write(ex);
            }
            catch (Exception ex)
            {
                throw LogWriter.Write(ex);
            }
        }

        public void DeleteOrder(DeliveryOrder order)
        {
            try
            {
                _DbContext.DeliveryOrders.DeleteOnSubmit(order);
                _DbContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw LogWriter.Write(ex);
            }
        }

        public DeliveryOrder GetOrderByID(Guid id)
        {
            DeliveryOrder result = null;

            try
            {
                result = _DbContext.DeliveryOrders.Where(e => e.DeliveryOrderID == id).FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw LogWriter.Write(ex);
            }

            return result;
        }

        public IList<DeliveryOrder> GetQueueForRefresh()
        {
            var result = _DbContext.DeliveryOrders.Where(
                e => e.IsAutoRefresh == true 
                    && (DateTime.Now.AddMinutes(15) - e.LastSummaryUpdatedTime) > new TimeSpan(0, Convert.ToInt32(e.RefreshDuration * 60), 0))
                    .ToList();
            return result;
        }

        public IList<DeliveryOrder> GetAll()
        {
            IList<DeliveryOrder> results = null;
            try
            {
                results = _DbContext.DeliveryOrders.OrderByDescending(e => e.AddDateTime).ToList();
            }
            catch (Exception ex)
            {
                throw LogWriter.Write(ex);
            }

            return results;
        }
    }
}
